//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmHestonHullWhiteVariancePart.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Experimental/Finitedifferences/TripleBandLinearOp.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Double sigma, Double kappa, Double theta) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phFdmHestonHullWhiteVariancePart = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        QuantLib::Real _sigma = (QuantLib::Real)ValueHelper::Convert (sigma);
        QuantLib::Real _kappa = (QuantLib::Real)ValueHelper::Convert (kappa);
        QuantLib::Real _theta = (QuantLib::Real)ValueHelper::Convert (theta);
        _ppFdmHestonHullWhiteVariancePart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart> (new QuantLib::FdmHestonHullWhiteVariancePart ( _mesher,  _sigma,  _kappa,  _theta ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteVariancePart = NULL;
#endif
	_ppFdmHestonHullWhiteVariancePart = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (QuantLib::FdmHestonHullWhiteVariancePart& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmHestonHullWhiteVariancePart = NULL;
#endif
	_ppFdmHestonHullWhiteVariancePart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart> (&childNative);
    
    _FdmHestonHullWhiteVariancePartOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (CFdmHestonHullWhiteVariancePart^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteVariancePart = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonHullWhiteVariancePart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteVariancePart = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonHullWhiteVariancePart::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (QuantLib::Handle<QuantLib::FdmHestonHullWhiteVariancePart>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteVariancePart = &childNative;
	_ppFdmHestonHullWhiteVariancePart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart>>(childNative.currentLink());
    
    _FdmHestonHullWhiteVariancePartOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (QuantLib::Handle<QuantLib::FdmHestonHullWhiteVariancePart> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteVariancePart = &childNative;
	_ppFdmHestonHullWhiteVariancePart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::CFdmHestonHullWhiteVariancePart (QuantLib::FdmHestonHullWhiteVariancePart childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteVariancePart = NULL;
#endif
	_ppFdmHestonHullWhiteVariancePart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart> (new QuantLib::FdmHestonHullWhiteVariancePart (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::~CFdmHestonHullWhiteVariancePart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmHestonHullWhiteVariancePart != NULL)
    {
	    delete _ppFdmHestonHullWhiteVariancePart;
        _ppFdmHestonHullWhiteVariancePart = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::!CFdmHestonHullWhiteVariancePart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmHestonHullWhiteVariancePart != NULL)
    {
	    delete _ppFdmHestonHullWhiteVariancePart;
    }
}
QuantLib::FdmHestonHullWhiteVariancePart& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::GetReference ()
{
    if (_ppFdmHestonHullWhiteVariancePart == NULL) throw gcnew NativeNullException ();
	return **_ppFdmHestonHullWhiteVariancePart;
}
boost::shared_ptr<QuantLib::FdmHestonHullWhiteVariancePart>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::GetShared ()
{
    if (_ppFdmHestonHullWhiteVariancePart == NULL) throw gcnew NativeNullException ();
	return *_ppFdmHestonHullWhiteVariancePart;
}
QuantLib::FdmHestonHullWhiteVariancePart* Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::GetPointer ()
{
    if (_ppFdmHestonHullWhiteVariancePart == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmHestonHullWhiteVariancePart;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonHullWhiteVariancePart>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::GetHandle ()
{
	if (_phFdmHestonHullWhiteVariancePart == NULL)
	{
		_phFdmHestonHullWhiteVariancePart = new Handle<QuantLib::FdmHestonHullWhiteVariancePart> (*_ppFdmHestonHullWhiteVariancePart);
	}
	return *_phFdmHestonHullWhiteVariancePart;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::HasNative () 
{
	return (_ppFdmHestonHullWhiteVariancePart != NULL);
}

Cephei::QL::Experimental::Finitedifferences::ITripleBandLinearOp^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart::GetMap::get ()
{
    try
    {
    	QuantLib::TripleBandLinearOp& _rv = (QuantLib::TripleBandLinearOp&)(*_ppFdmHestonHullWhiteVariancePart)->getMap ( );   
        Cephei::QL::Experimental::Finitedifferences::CTripleBandLinearOp^ _nrv = gcnew Cephei::QL::Experimental::Finitedifferences::CTripleBandLinearOp (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmHestonHullWhiteVariancePart^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteVariancePart_Factory::Create (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Double sigma, Double kappa, Double theta)
{
    return gcnew CFdmHestonHullWhiteVariancePart ( mesher,  sigma,  kappa,  theta);
}
